package cn.com.xf.web.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.json.simple.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.com.xf.common.AbstractBaseController;
import cn.com.xf.domain.Tbsecuritycheck;
import cn.com.xf.service.TbsecuritycheckService;
import cn.com.xf.utils.ATest;
import cn.com.xf.utils.DateUtils;
import net.sf.json.JSONArray;

@Controller
@RequestMapping("/tbsecurity")
public class TbsecurityController extends AbstractBaseController{
	
	private final static String  check_key = "{C0348565-DF02-E1F7-0898-AE78D8402E9B}";
	Log log = LogFactory.getLog(CheckDataController.class);
	
	@Autowired
	private TbsecuritycheckService tbsecuritycheckService;
	
	@RequestMapping(value = "/checkcount", method = RequestMethod.POST )
	@ResponseBody
	public void checkcount(HttpServletRequest request, HttpServletResponse response, @RequestBody String body)
			throws Exception {
		//System.out.println("服务端接受到数据");
		request.setCharacterEncoding("utf-8");
		String result = null;
		Object[] data = {};
		net.sf.json.JSONObject josnBody = net.sf.json.JSONObject.fromObject(body);
		String body_sign = josnBody.get("sign")+"";
		String body_data = josnBody.get("data")+"";
		if(!body_sign.equals(ATest.getMd5Sign(check_key+body_data))){
			//System.out.println("sign错误");
			Map<String,Object> map = new HashMap<String,Object>();
			map.put("status", "1");
			map.put("error", "接收端sign有误");
			map.put("data", data);
			result = JSONObject.toJSONString(map);
			response.setCharacterEncoding("UTF-8");
			response.getWriter().println(result);
		}else{
			Map<String,Object> resultMap = new HashMap<>();
			resultMap.put("status", "0");
			resultMap.put("error", "");
			List<Map<String,Object>> list = new ArrayList<>();
			List<Tbsecuritycheck> listCheck = new ArrayList<>();
			JSONArray arr_data = JSONArray.fromObject(body_data);
			for(int i = 0; i < arr_data.size(); i++){
				Map<String,Object> jsonmap = new HashMap<>();
				Tbsecuritycheck tc = new Tbsecuritycheck();
				net.sf.json.JSONObject jsonObj = arr_data.getJSONObject(i);
				tc.setEntersite(jsonObj.getString("entersite"));
				tc.setScanclass(jsonObj.getString("scanclass"));
				tc.setScandate(DateUtils.parseDate(jsonObj.getString("scandate")));
				tc.setScanqty(Integer.parseInt(jsonObj.getString("scanqty")));
				tc.setEntermacid(jsonObj.getString("entermacid"));
				tc.setEntertime(new Date());
				tc.setEnteruser(jsonObj.getString("enteruser"));
				listCheck.add(tc);
				jsonmap.put("sta", "OK");
				jsonmap.put("err", "");
				list.add(jsonmap);		
			}
		    try {
			   tbsecuritycheckService.insertBatch(listCheck);
			} catch (Exception e) {
				log.error("安检同步功能不能插入数据库："+e.getMessage());
				resultMap.put("error", "安检同步数据不能插入数据库失败");
				resultMap.put("status", "1");
			}finally {
			}
		    String resultdata = JSONArray.fromObject(list).toString();
			resultMap.put("data", resultdata);
			result = JSONObject.toJSONString(resultMap);
			response.setCharacterEncoding("UTF-8");
			response.getWriter().println(result);
		}	
	}
	
	@RequestMapping(value ="/isUpload", method = RequestMethod.POST )
	@ResponseBody
	public void isUpload(HttpServletRequest request, HttpServletResponse response, @RequestBody String body)
			throws Exception {
		request.setCharacterEncoding("utf-8");
		String result = null;
		Map<String,Object> data =null;
		net.sf.json.JSONObject josnBody = net.sf.json.JSONObject.fromObject(body);
		String body_sign = josnBody.get("sign")+"";
		String body_data = josnBody.get("data")+"";
		if(!body_sign.equals(ATest.getMd5Sign(check_key+body_data))){
			//System.out.println("sign错误");
			Map<String,Object> map = new HashMap<String,Object>();
			map.put("status", "1");
			map.put("error", "接收端sign有误");
			map.put("data", data);
			result = JSONObject.toJSONString(map);
			response.setCharacterEncoding("UTF-8");
			response.getWriter().println(result);
		}else{
			net.sf.json.JSONObject jBody = net.sf.json.JSONObject.fromObject(body_data);
			Map<String,Object> resultMap = new HashMap<>();
			data = new HashMap<String,Object>();
			resultMap.put("status", "0");
			resultMap.put("error", "");
			String scandate = jBody.getString("scandate")+"";
			String scanclass =jBody.getString("scanclass")+"";
			String entersite =jBody.getString("entersite")+"";
			//System.out.println(scandate);
			//System.out.println(scanclass);
			//System.out.println(entersite);
			int count = tbsecuritycheckService.selectCount(scandate, scanclass,entersite);
			if(count>0){
				data.put("sta","n");
			}else{
				data.put("sta","y");
			}
			resultMap.put("data",data);
			result = JSONObject.toJSONString(resultMap);
			response.setCharacterEncoding("UTF-8");
			response.getWriter().println(result);
		}
		
	}
}
